package xinyu.customer.base;

import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.logos.common.base.BaseActivity;
import com.logos.common.widget.display.DisplayHelper;
import com.logos.common.widget.display.StatusBarHelper;
import com.netease.nim.uikit.common.fragment.TFragment;

import java.util.ArrayList;
import java.util.List;

import xinyu.customer.R;
import xinyu.customer.widget.dialog.CustomProgressDialog;

public class AppBaseActivity extends BaseActivity {

    private CustomProgressDialog mLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarHelper.translucent(this);
        StatusBarHelper.setStatusBarLightMode(this);
    }

    protected void measureTitleBarHeight(View view) {
        if (view != null) {
            view.setPadding(view.getLeft(), DisplayHelper.getStatusBarHeight(this), view.getRight(), view.getBottom());
        }
    }

    @Override
    protected void onPause() {
//        MobclickAgent.onPause(getContext());
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        MobclickAgent.onResume(getContext());
    }

    /**
     * showLoading
     */
    public void showLoadingDialog() {
        if (isFinishing()) {
            return;
        }
        if (null == mLoading) {
            mLoading = new CustomProgressDialog(this, getString(R.string.loading_msg), true, true);
        }
    }

    /**
     * hideLoading
     */
    public void hideLoadingDialog() {
        if (isFinishing()) {
            return;
        }
        if (mLoading != null && mLoading.isShowing()) {
            mLoading.dismiss();
            mLoading = null;
        }
    }

    /**
     * fragment management
     */
    public TFragment addFragment(TFragment fragment) {
        List<TFragment> fragments = new ArrayList<>(1);
        fragments.add(fragment);

        List<TFragment> fragments2 = addFragments(fragments);
        return fragments2.get(0);
    }

    public List<TFragment> addFragments(List<TFragment> fragments) {
        List<TFragment> fragments2 = new ArrayList<>(fragments.size());

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        boolean commit = false;
        for (int i = 0; i < fragments.size(); i++) {
            // install
            TFragment fragment = fragments.get(i);
            int id = fragment.getContainerId();

            // exists
            TFragment fragment2 = (TFragment) fm.findFragmentById(id);

            if (fragment2 == null) {
                fragment2 = fragment;
                transaction.add(id, fragment);
                commit = true;
            }

            fragments2.add(i, fragment2);
        }

        if (commit) {
            try {
                transaction.commitAllowingStateLoss();
            } catch (Exception e) {

            }
        }

        return fragments2;
    }

    public TFragment switchContent(TFragment fragment) {
        return switchContent(fragment, false);
    }

    protected TFragment switchContent(TFragment fragment, boolean needAddToBackStack) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(fragment.getContainerId(), fragment);
        if (needAddToBackStack) {
            fragmentTransaction.addToBackStack(null);
        }
        try {
            fragmentTransaction.commitAllowingStateLoss();
        } catch (Exception e) {

        }

        return fragment;
    }
    protected <T extends View> T findView(int resId) {
        return (T) (findViewById(resId));
    }

}
